/**
 * Project Name:fastPlat
 * File Name:CompanyServiceImpl.java
 * Package Name:com.fast.user.service.admin.impl
 * Date:2014年12月15日上午10:54:08
 * Copyright (c) 2014, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.user.service.dept.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fast.base.dao.daoSuport.JpaDaoSupport;
import com.fast.user.model.dept.Company;
import com.fast.user.service.dept.CompanyService;

/**
 * ClassName:CompanyServiceImpl <br/>
 * 用途: 公司信息服务 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @author lushuifa242
 * @version  
 * @since    JDK 1.6
 * @see
 * Date:     2014年12月15日 上午10:54:08 <br/> 	 
 */
@Service("companyService")
public class CompanyServiceImpl extends JpaDaoSupport implements CompanyService  {
	
	/**
	 * 说明: 获取企业机构树 <br>
	 * 编写者:lushuifa
	 * 创建时间:2017年11月4日 下午11:20:19 </pre>
	 * @param 
	 * @author lushuifa242
	 * @return JSONArray 说明
 	 */
	@Override
	public JSONArray getCompanyTree(String parentUUID){
		JSONArray topArr = new JSONArray();
		try {
			//父节点
			JSONObject parentNode = new JSONObject();
			parentNode.put("id", "0");
			parentNode.put("text","根目录");
			parentNode.put("state","open");
			parentNode.put("iconCls","icon-folderPage");
			JSONArray jsonArray = handCompanyTree(parentUUID);
			parentNode.put("children", jsonArray);
			topArr.add(parentNode);
			return topArr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return topArr;
	}

	private JSONArray handCompanyTree(String parentUUID) {
		JSONArray jsonArray = new JSONArray();
		if(StringUtils.isBlank(parentUUID)){
			parentUUID = "0";
		}
		JSONObject jsonOBject = null;
		List<Company> companyList = super.queryForBeanList(Company.class, " where parentUUID='"+parentUUID+"' and delete_flag=0 order by orderNumber asc");
		if(companyList.size()>0){
			for (Company company : companyList) {
			    boolean exists = super.isExist(Company.class, "where parentUUID='"+company.getUuid()+"' and delete_flag=0");
			    jsonOBject = new JSONObject();
			    jsonOBject.put("id", company.getUuid());
				jsonOBject.put("text",company.getCompanyName());
				jsonOBject.put("iconCls",exists?"icon-folderPage":"icon-page");
				if(exists){
					jsonOBject.put("children", handCompanyTree(company.getUuid()));
				}
				//添加其它属性
				jsonOBject.put("attributes", null);
				jsonArray.add(jsonOBject);
			}
		}
		return jsonArray;
	}
	
	/**
	 * 说明: 单位列表 分级显示<br>
	 * 编写者:lushuifa
	 * 创建时间:2017年11月12日 下午11:11:26 </pre>
	 * @param 
	 * @author lushuifa242
	 * @return List<Map<String,Object>> 说明
	 */
	@Override
	public List<Map<String, Object>> zzCompanList(JSONArray jsonArray,int index) {
		List<Map<String,Object>> beanList = new ArrayList<Map<String,Object>>();
		Map<String,Object> objectMap = null;
		index++;
		for (Object o : jsonArray) {
			JSONObject jsonObject = (JSONObject) o;
			objectMap = new HashMap<String,Object>(); 
			objectMap.put("uuid", jsonObject.getString("id"));
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < index; i++) {
				sb.append("-");
			}
			objectMap.put("companyName", sb.toString()+jsonObject.getString("text"));
			beanList.add(objectMap);
			if(jsonObject.containsKey("children")){
				JSONArray childrens = jsonObject.getJSONArray("children");
				if(null!=childrens&&!childrens.isEmpty()){
					beanList.addAll(zzCompanList(childrens,index));
				}
			}
		}
		return beanList;
	}
     
}

